<template>
  <div class="app-container">
    <div class="head-container">
      <!-- 搜索 -->
      <div v-show="sup_this.searchToggle">
        <el-input v-model="paramBean.sysGenCode" clearable placeholder="采购单号" style="width:200px" class="filter-item" @keyup.enter.native="toQuery"/>
        <el-input v-model="paramBean.itemCode" clearable placeholder="产品编码" style="width:200px" class="filter-item" @keyup.enter.native="toQuery"/>
        <el-button class="filter-item" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" type="warning" icon="el-icon-refresh-left" @click="resetQuery()">重置</el-button>
      </div>
      <div class="crud-opts">
        <el-button
          v-permission="['SYS_ADMIN','COMMON_ADMIN','STOCKIN_PO_REJECT_ALL','STOCKIN_PO_REJECT_CREATE']"
          class="filter-item"
          type="primary"
          @click="toStockinPoReject">采购退换货</el-button>
        <DataGridOpts :sup_this="sup_this" @refreshClick="init" @searchClick="toggleSearch"/>
      </div>
    </div>
    <!--表格渲染-->
    <DataGridTable
      v-loading="loading"
      ref="mainTable"
      :sup_this="sup_this"
      :data="data.rtnList"
      :total="total"
      :all-define-columns="['itemName','qty','receiveQty','balanceQty','rejectQty', 'deliveryDate','itemSpec','itemBrand','unit','taxPrice','taxRate']"
      :stripe="false"
      :row-class-name="custTableRowClassName"
      show-cell-class
      show-row-number
      grid-id="customReceivePoMngGridId"
      @selection-change="handleGridSelectionChange">
      <DataGridColumnSlot prop="busDate" label="业务时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.busDate) }}</span>
        </template>
      </DataGridColumnSlot>
      <DataGridColumnSlot prop="sysGenCode" label="采购单号">
        <template slot-scope="scope">
          <edit v-if="viewPoPriv" :data="scope.row" :sup_this="sup_this" />
          <span v-else>{{ scope.row.sysGenCode }}</span>
        </template>
      </DataGridColumnSlot>
      <DataGridColumn prop="busOtherCpyName" label="客户" />
      <DataGridColumn prop="itemCode" label="物料编号" width="150px" />
      <DataGridColumn prop="itemName" label="名称" />
      <DataGridColumn slot="qty" prop="qty" label="采购数" />
      <DataGridColumn slot="receiveQty" prop="receiveQty" label="已收货数" />
      <DataGridColumn slot="balanceQty" prop="balanceQty" label="待收货数" />
      <DataGridColumn slot="rejectQty" prop="rejectQty" label="退货数" />
      <DataGridColumnSlot slot="deliveryDate" prop="deliveryDate" label="交货日期">
        <template slot-scope="scope">
          <span>{{ parseDate(scope.row.deliveryDate) }}</span>
        </template>
      </DataGridColumnSlot>
      <DataGridColumn slot="itemSpec" prop="itemSpec" label="规格" />
      <DataGridColumn slot="itemBrand" prop="itemBrand" label="品牌" />
      <DataGridColumnSlot slot="unit" prop="unit" label="单位">
        <template slot-scope="scope">
          <template v-for="item in dicts.item_unit">
            <span v-if="scope.row.unit.toString() === item.dictCode" :key="item.id" >{{ item.dictName }}</span>
          </template>
        </template>
      </DataGridColumnSlot>
      <DataGridColumn v-permission="['SYS_ADMIN','COMMON_ADMIN','PURCHASE_PO_ALL','PURCHASE_PO_PRICE']" slot="taxPrice" prop="taxPrice" label="含税单价" />
      <DataGridColumn v-permission="['SYS_ADMIN','COMMON_ADMIN','PURCHASE_PO_ALL','PURCHASE_PO_PRICE']" slot="taxRate" prop="taxRate" label="税率" />
    </DataGridTable>
    <eForm ref="form" :sup_this="sup_this" />
    <poForm ref="poForm" :sup_this="sup_this" />
  </div>
</template>

<script>
import { parseTime, parseDate } from '@/utils/index'
import edit from '@/views/prod/stockin/module/customPoEdit'
import eForm from './module/poForm'
import poForm from '@/views/purchase/po/module/form'
import initData from '@/mixins/initData'
import po from '@/api/business/Po'
import checkPermission from '@/utils/permission'

export default {
  components: { poForm, eForm, edit },
  dictTypes: ['item_unit'],
  userDictTypes: ['brand_type'],
  mixins: [initData],
  props: {
  },
  data() {
    return {
      delLoading: false,
      sup_this: this,
      viewPoPriv: false
    }
  },
  created() {
    this.viewPoPriv = checkPermission(['SYS_ADMIN', 'COMMON_ADMIN', 'PURCHASE_PO_ALL', 'PURCHASE_PO_CREATE'])
    this.url = po.loadDetailDataUrl()
    this.defaultParamBean.selfState = 10 // 默认加载草稿，待审批，审批不通过的数据
    this.defaultParamBean.balanceQty = 1
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    parseDate,
    checkPermission,
    toStockinPoReject() {
      const _this = this.sup_this
      const busRelList = []
      let cpyKey = null
      let cpyDiffFlag = false
      _this.selectedGridRows.forEach(row => {
        if (cpyKey && cpyKey !== row.otherSysOwnerCpy) {
          cpyDiffFlag = true
          return
        }
        cpyKey = row.otherSysOwnerCpy
        busRelList.push({ fromBusModule: row.busModule, fromBusClass: row.busClass, fromId: row.mainId, fromCode: row.sysGenCode, fromDetailLotNo: row.detailLotNo })
      })
      if (cpyDiffFlag) {
        this.$message({
          message: '请选择相同供应商',
          type: 'warning'
        })
        return
      }
      if (busRelList.length === 0) {
        this.$message({
          message: '请选择记录',
          type: 'warning'
        })
        return
      }
      const _thisform = this.$refs.form
      _thisform.initDataOfBefore()
      _thisform.addDataFromPoReject(busRelList)
    },
    custTableRowClassName({ row, rowIndex }) {
      if (row.balanceQty > 0) {
        return 'warning-row'
      }
      return ''
    }
  }
}
</script>

<style>
.el-table .warning-row {
  background: #fdd48a;
}
</style>
